<?php

namespace App\Jobs;

use EasyWeChat\Factory;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

class EnrollSuccess implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $userInfo;
    protected $courseInfo;
    protected $campusInfo;
    protected $courseSection;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($userInfo, $courseInfo, $campusInfo, $courseSectionDetail = [])
    {
        $this->userInfo      = $userInfo;
        $this->courseInfo    = $courseInfo;
        $this->campusInfo    = $campusInfo;
        $this->courseSection = $courseSectionDetail;
    }

    /**
     * @return bool
     */
    public function handle()
    {
        //服务通知加一个
        $app      = Factory::miniProgram(config('wechat.mini_program.default'));
        $toUser   = $this->userInfo['openid'];
        $sendData = [
            'touser'      => $toUser,
            'template_id' => 'U5BdigRmMI4dRoZSg9sh5pryRYHbeCK-G-HRiuBbjFM',
            'data'        => [
                'thing41' => $this->courseInfo['name'],
                'time43'  => date('Y年m月d日',strtotime($this->courseSection['class_time'])).' '.date('H:i',strtotime($this->courseSection['begin_time'])),
                'thing44' => $this->campusInfo['address'],
                'thing4'  => '记得去上课哟，若日程有变，可取消报名',
            ],
            'page'        => 'pages/campus/index'
        ];

        //发送给用户对应的关联者
        $rel = $app->subscribe_message->send($sendData);
        Log::info('报名成功通知结果', ['rel' => json_encode($rel), 'send_data' => json_encode($sendData['data'])]);

        return true;
    }
}
